<?php
/**
* @package Frontend-User-Access (com_frontenduseraccess)
* @version 3.0.8
* @copyright Copyright (C) 2008 Carsten Engel. All rights reserved.
* @license GPL versions free/trial/pro
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

if(!$this->controller->fua_config['display_modules'] && $this->controller->user_type!='Super Administrator'){
	die('Restricted access');
}

//header and nav
$this->controller->echo_header();

$fua_modules_array = array();
foreach($this->fua_modules_object as $fua_module){
	$fua_module_id = $fua_module->id;
	$fua_module_title = $fua_module->title;		
	$fua_modules_array[] = array($fua_module_id, $fua_module_title);	
}	

//make javascript array from components
$javascript_array_modules = 'var modules = new Array(';
for($n = 0; $n < count($fua_modules_array); $n++){	
	if($n==0){
		$first = false;
	}else{
		$javascript_array_modules .= ',';
	}
	$javascript_array_modules .= "'".$fua_modules_array[$n][0]."'";
}	
$javascript_array_modules .= ');';

//echo $javascript_array_modules;
//echo $n;
?>
<script language="javascript" type="text/javascript">

<?php echo $javascript_array_modules."\n"; ?>

function select_all(usergroup_id, select_all_id){
	action = document.getElementById(select_all_id).checked;		
	for (i = 0; i < modules.length; i++){
		box_id = modules[i]+'__'+usergroup_id;
		hidden_id = modules[i]+'__'+usergroup_id+'__hidden';
		if(action==true){
			document.getElementById(box_id).checked = true;
			document.getElementById(hidden_id).value = hidden_id+'__1';
		}else{
			document.getElementById(box_id).checked = false;
			document.getElementById(hidden_id).value = hidden_id+'__';
		}
	}	
}

function toggle_right(hidden_field_id){
	field = document.getElementById(hidden_field_id);
	if(field.value==hidden_field_id+'__1'){
		field.value = hidden_field_id+'__';
	}else{
		field.value = hidden_field_id+'__1';
	}
}

function check_limitstart(){
	ori_search = '<?php echo $this->search;?>';	
	new_search = document.getElementById('search').value;	
	if(ori_search!=new_search){
		document.adminForm.limitstart_needs_reset.value = 1;
	}else{
		document.adminForm.limitstart_needs_reset.value = 0;
	}	
	return true;
}

function really_delete_old_module_table(){
	if(confirm("<?php echo addslashes(JText::_('SURE_DELETE_MODULE_TABLE')); ?>.")){
		document.location.href = 'index2.php?option=com_frontenduseraccess&task=delete_old_module_table&<?php echo JUtility::getToken(); ?>=1';
	}
}

</script>
<form name="adminForm" method="post" action="">
		<input type="hidden" name="option" value="com_frontenduseraccess" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="limitstart_needs_reset" value="" />			
		<?php echo JHTML::_( 'form.token' ); ?>	
<table id="fua_subheader">
	<tr>
		<td>
			<?php 
				echo '<p>'.JText::_('MODULES_INFO').'. '.JText::_('MODULES_INFO3').'.</p>';
				
						
				$config = JFactory::getConfig();
				$database_name = $config->getValue('db');
				$tables_array = array();							
				$this->controller->db->setQuery("SHOW TABLES FROM `".$database_name."` ");
				$tables_array = $this->controller->db->loadResultArray();				
				$prefix = $config->getValue('dbprefix');
				$table_name = $prefix.'fua_modules';									
					
				if(in_array($table_name, $tables_array)){
				
					echo '<div style="border: 1px solid red; padding: 5px;">';
					echo '<img src="../includes/js/ThemeOffice/warning.png" alt="read this carefully" /> ';
					echo JText::_('OLD_MODULE_INFO').'. '.JText::_('OLD_MODULE_INFO2').'.<br /><br />';
					
				
					//get usergroups from db
					$this->controller->db->setQuery("SELECT * FROM #__fua_usergroups ORDER BY name");
					$fua_usergroups = $this->controller->db->loadObjectList();
				
					//get module access from db
					$this->controller->db->setQuery("SELECT module_groupid FROM #__fua_modules");
					$access_modules_old = $this->controller->db->loadResultArray();
					
					//print_r($access_modules_old);
					
					//get fua wrapper modules from database												
					$this->controller->db->setQuery("SELECT id, title, params FROM #__modules WHERE module='mod_frontend_user_access' ORDER BY title");
					$fua_modules_object2 = $this->controller->db->loadObjectList();	
					
					//print_r($fua_modules_object);
					
					$fua_modules_array2 = array();
					foreach($fua_modules_object2 as $fua_module){
						$fua_module_id = $fua_module->id;
						$fua_module_title = $fua_module->title;	
						//get the id of the module to load in the wrapper
						$fua_module_params_temp = $fua_module->params;
						$fua_module_params = explode( "\n", $fua_module_params_temp);
						if(count($fua_module_params)>1){
							for($n = 0; $n < 3; $n++){		
								list($var,$value) = split('=',$fua_module_params[$n]); 
								$values[$var] = trim($value);	
							}	
							$fua_module_load_id = $values['fua_load_mod_id'];	
						}else{
							$fua_module_load_id = '';
						}	
						//$fua_load_module_title = get_load_module_title($fua_module_load_id);
						$this->controller->db->setQuery("SELECT title FROM #__modules WHERE id='$fua_module_load_id' LIMIT 1");
						$fua_load_module_title = $this->controller->db->loadResult();	
						$fua_modules_array2[] = array($fua_module_id, $fua_module_title, $fua_module_load_id, $fua_load_module_title);	
					}
					
					echo '<table width="95%">';
					echo '<tr class="row0">';
					echo '<th align="left">'.JText::_('OLD_WRAPPER_DATA').'</td>';				
					//$this->controller->loop_usergroups($this->fua_usergroups);
					foreach($fua_usergroups as $fua_usergroup){
						echo '<th style="text-align: center;">';
						echo $fua_usergroup->name;
						echo '</th>';
					}			
					echo '</tr>';
					$k = 1;
					for($n = 0; $n < count($fua_modules_array2); $n++){			
						echo '<tr class="row'.$k.'"><td>['.$fua_modules_array2[$n][0].'] '.$fua_modules_array2[$n][1].' (';
						if($fua_modules_array2[$n][2]!=''){
							echo ' ['.$fua_modules_array2[$n][2].'] ';
						}
						echo $fua_modules_array2[$n][3].')</td>';			
						foreach($fua_usergroups as $fua_usergroup){
							$checked = '';
							if (in_array($fua_modules_array2[$n][0].'__'.$fua_usergroup->id, $access_modules_old)) {
								$checked = 'checked="checked"';
							}
							echo '<td align="center"><input type="checkbox" name="bogus[]" value="" '.$checked.' /></td>';
						}
						echo '</tr>';
						if($k==1){
							$k = 0;
						}else{
							$k = 1;
						}	
					}
					if(count($fua_modules_array2)==0){
						echo '<tr>';
						echo '<td>';
						echo JText::_('NO_OLD_DATA_FOUND').'.';
						echo '</td>';
						echo '</tr>';
					}
					echo '</table>';
					echo '<br /><br /><input type="button" onclick="really_delete_old_module_table();" value="'.JText::_('DONE_UPDATING_DELETE_OLD_TABLE').'" />';
					echo '</div>';
				}//end if old table has been found

				//legend and message if reverse access	
				$this->controller->reverse_access_warning('modules_reverse_access');
				
				//message in free version that these restrictions will not work in free version
				$this->controller->not_in_free_version();					
				
				//message if component access is not activated
				if($this->controller->fua_config['modules_active']==false){				
					echo '<div style="color: red; text-align: left;">'.JText::_('NO_MODULES_ACTIVE').'. <a href="index2.php?option=com_frontenduseraccess&view=config&tab=module_access">'.JText::_('ACTIVATE_IN_CONFIG').'</a><br/><br/></div>';
				}
						
			?>
		</td>
		<td id="td_usergroup_selector">
			<?php echo $this->controller->usergroup_selector(); ?>
		</td>
	</tr>
</table>	
<table class="adminlist">
	<tr>		
		<th align="left" colspan="2">&nbsp;
		</th>
		<?php			
			$this->controller->loop_usergroups($this->fua_usergroups);			
		?>			
	</tr>
		
	<?php
		
		//row with select_all checkboxes
		echo '<tr class="row0">';
		echo '<td align="left" colspan="2">'.JText::_('SELECTALL').'</td>';
		foreach($this->fua_usergroups as $fua_usergroup){
			echo '<td style="text-align:center;"><input type="checkbox" name="checkall[]" value="" id="checkall_'.$fua_usergroup->id.'" onclick="select_all('.$fua_usergroup->id.',this.id);" /></td>';
		}
		echo '</tr>';
				
		$k = 1;		
		$counter = 0;	
		for($n = 0; $n < count($fua_modules_array); $n++){			
			echo '<tr class="row'.$k.'"><td class="column_ids">'.$fua_modules_array[$n][0].'</td><td>'.$fua_modules_array[$n][1];
			echo '</td>';			
			foreach($this->fua_usergroups as $fua_usergroup){
				$checked = '';
				$checked_hidden = '';
				if (in_array($fua_modules_array[$n][0].'__'.$fua_usergroup->id, $this->access_modules)) {
					$checked = 'checked="checked"';					
					$checked_hidden = '1';
				}
				echo '<td align="center"><input type="hidden" name="module_access_hidden[]" id="'.$fua_modules_array[$n][0].'__'.$fua_usergroup->id.'__hidden" value="'.$fua_modules_array[$n][0].'__'.$fua_usergroup->id.'__hidden__'.$checked_hidden.'" /><input type="checkbox" name="module_access[]" id="'.$fua_modules_array[$n][0].'__'.$fua_usergroup->id.'" onclick="toggle_right(\''.$fua_modules_array[$n][0].'__'.$fua_usergroup->id.'__hidden\');" value="'.$fua_modules_array[$n][0].'__'.$fua_usergroup->id.'" '.$checked.' /></td>';
			}
			echo '</tr>';
			if($k==1){
				$k = 0;
			}else{
				$k = 1;
			}			
			if($counter==7){
				echo '<tr><th colspan="2">&nbsp;</th>';	
				$this->controller->loop_usergroups($this->fua_usergroups);
				echo '</tr>';
				$counter = 0;
			}
			$counter = $counter+1;	
		}
	echo '<table class="adminlist"><tfoot><tr><td>'.$this->pagination->getListFooter().'</td></tr></tfoot></table>';
	?>
			
</table>
</form>
<?php
$this->controller->display_footer();
?>